package cn.ztuo.bitrade.service;


import cn.ztuo.bitrade.dao.IeoOdfrecordDao;
import cn.ztuo.bitrade.entity.IeoOdfrecord;
import cn.ztuo.bitrade.entity.QIeoOdfrecord;
import cn.ztuo.bitrade.service.Base.BaseService;
import cn.ztuo.bitrade.util.DateUtil;
import cn.ztuo.bitrade.util.PredicateUtils;
import cn.ztuo.bitrade.vo.EmptionRecrodVO;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: fiwork.net
 * @Date: 2019/4/26 4:00 PM
 */
@Service
public class IeoOdfrecordService extends BaseService{

    @Autowired
    private IeoOdfrecordDao emptionRecordDao;


    public IeoOdfrecord save(IeoOdfrecord emptionRecord){
        return emptionRecordDao.save(emptionRecord);
    }
    

    public IeoOdfrecord findById(Long id){
        return emptionRecordDao.findOne(id);
    }

    public List<Long> selectTypes(Long id,List<Integer> types){
        return emptionRecordDao.selectTypes(id,types);
    }

    public List<IeoOdfrecord> findAllByTypeNotAndStatus(Integer type,String status){
        return emptionRecordDao.findAllByTypeNotAndStatus(type,status);
    }

    public Page<IeoOdfrecord> getByPage(EmptionRecrodVO emptionRecrodVO) throws Exception{
        ArrayList<BooleanExpression> booleanExpressions = new ArrayList<>();
        if (StringUtils.isNotEmpty(emptionRecrodVO.getStartTime())){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.createTime.goe(DateUtil.stringToDate(emptionRecrodVO
                    .getStartTime())));
        }
        if (StringUtils.isNotEmpty(emptionRecrodVO.getEndTime())){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.expectTime.loe(DateUtil.stringToDate(emptionRecrodVO
                    .getEndTime())));
        }
        if (StringUtils.isNotEmpty(emptionRecrodVO.getIeoName())){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.ieoName.like("%"+emptionRecrodVO.getIeoName()+"%"));
        }
        if (StringUtils.isNotEmpty(emptionRecrodVO.getUserName())){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.userName.like("%"+emptionRecrodVO.getUserName()+"%"));
        }
        if (StringUtils.isNotEmpty(emptionRecrodVO.getStatus())){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.status.eq(emptionRecrodVO.getStatus()));
        }
        if (emptionRecrodVO.getUserId() != null){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.userId.eq(emptionRecrodVO.getUserId()));
        }
        if(emptionRecrodVO.getSaleCoin() != null){
            booleanExpressions.add(QIeoOdfrecord.ieoOdfrecord.saleCoin.eq(emptionRecrodVO.getSaleCoin()));
        }
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(emptionRecrodVO.getPageNum() - 1, emptionRecrodVO.getPageSize(), sort);
        return emptionRecordDao.findAll(predicate,pageable);

    }

    public BigDecimal sumAmount(Long id){
        return emptionRecordDao.sumAmount(id);
    }
}
